package com.fr.base.core.list;

import com.fr.base.FRContext;
import com.fr.base.file.CacheManager;
import com.fr.util.Utils;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MBBTable
{
  private File diskDirectory = null;
  private transient List[] mbbLists = null;
  private int MEM_SIZE = FRContext.getCacheManager().getMaxMemSize();

  public MBBTable(int paramInt1, int paramInt2)
  {
    this.mbbLists = new MBBList[paramInt1];
    this.diskDirectory = getCacheDirectory();
    int i = 3145727;
    if (paramInt1 > 0)
      i = this.MEM_SIZE / paramInt1;
    for (int j = 0; j < this.mbbLists.length; ++j)
    {
      String str = "MBB_Column";
      File localFile = new File(this.diskDirectory, str + "_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D));
      for (int k = 0; localFile.exists(); ++k)
        localFile = new File(this.diskDirectory, str + "_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D) + k);
      localFile.deleteOnExit();
      this.mbbLists[j] = new MBBList(localFile, paramInt2, i);
    }
  }

  public void addValueAt(int paramInt, Object paramObject)
  {
    this.mbbLists[paramInt].add(paramObject);
  }

  public Object getValueAt(int paramInt1, int paramInt2)
  {
    if ((paramInt1 > columnCount()) && (paramInt1 >= 0))
    {
      FRContext.getLogger().log(Level.WARNING, "Index out of bounds");
      return null;
    }
    return this.mbbLists[paramInt1].get(paramInt2);
  }

  public int columnCount()
  {
    return this.mbbLists.length;
  }

  public int rowCount()
  {
    return this.mbbLists[0].size();
  }

  public void clear()
  {
    int i;
    if (this.mbbLists != null)
      for (i = 0; i < this.mbbLists.length; ++i)
        if (this.mbbLists[i] != null)
          this.mbbLists[i].clear();
    System.gc();
    if ((this.diskDirectory != null) && (!(Utils.deleteFile(this.diskDirectory))));
  }

  private File getCacheDirectory()
  {
    if (this.diskDirectory == null)
    {
      this.diskDirectory = new File(FRContext.getCacheManager().getCacheDirectory(), "DATA_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D));
      for (int i = 0; this.diskDirectory.exists(); ++i)
        this.diskDirectory = new File(FRContext.getCacheManager().getCacheDirectory(), "DATA_" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D) + i);
      Utils.mkdirs(this.diskDirectory);
      this.diskDirectory.deleteOnExit();
    }
    return this.diskDirectory;
  }

  protected void finalize()
    throws Throwable
  {
    super.finalize();
    clear();
  }
}